#ifndef NYU_REFCNT_H_
#define NYU_REFCNT_H_

#include <glib.h>

#define NYU_REFCNT(X) ((NyuRefcnt *)(X))
#define NYU_REFCNT_INIT(X,Y) {                          \
        NYU_REFCNT(X)->size = sizeof(*(X));             \
        NYU_REFCNT(X)->count = 1;                       \
        NYU_REFCNT(X)->destroy = (NyuRefcntDestroy)(Y); \
    }

typedef struct NyuRefcnt NyuRefcnt;
typedef void (*NyuRefcntDestroy)(gpointer self);

struct NyuRefcnt {
    guint size;
    gint count;
    NyuRefcntDestroy destroy;
};

gpointer nyu_refcnt_ref(gpointer self);
void nyu_refcnt_unref(gpointer self);

#if 0
#define nyu_refcnt_new(X) g_new0(X, 1)
#define nyu_refcnt_free g_free
#else
#define nyu_refcnt_new(X) g_slice_new0(X)
#define nyu_refcnt_free(self) g_slice_free1((self)->size, self)
#endif

#endif /* NYU_REFCNT_H_ */
